package query

import (
	"d-blog/pkg/util"
	"github.com/kataras/iris/v12"
)

type Params struct {
	Condition
	Context iris.Context
}

func NewQueryParams(ctx iris.Context) *Params {
	return &Params{Context: ctx}
}

func (receiver *Params) getValueByColumn(column string) string {
	if receiver.Context == nil {
		return ""
	}
	fieldName := util.ToLowerCamel(column)
	return receiver.Context.FormValue(fieldName)
}

func (receiver *Params) EqByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.Eq(column, value)
	}
	return receiver
}
func (receiver *Params) NotEqByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.NotEq(column, value)
	}
	return receiver
}

func (receiver *Params) GtByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.Gt(column, value)
	}
	return receiver
}

func (receiver *Params) GteByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.Gte(column, value)
	}
	return receiver
}

func (receiver *Params) LtByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.Lt(column, value)
	}
	return receiver
}

func (receiver *Params) LteByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.Lte(column, value)
	}
	return receiver
}

func (receiver *Params) LikeByReq(column string) *Params {
	value := receiver.getValueByColumn(column)
	if len(value) > 0 {
		receiver.Like(column, value)
	}
	return receiver
}

/*func (receiver *QueryParams) PageByReq() *QueryParams {
	if receiver.Context == nil {
		return receiver
	}
	paging := GetPaging(receiver.Ctx)
	receiver.Page(paging.Page, paging.Limit)
	return receiver
}*/

func (receiver *Params) Asc(column string) *Params {
	receiver.Orders = append(receiver.Orders, OrderByCol{Column: column, Asc: true})
	return receiver
}

func (receiver *Params) Desc(column string) *Params {
	receiver.Orders = append(receiver.Orders, OrderByCol{Column: column, Asc: false})
	return receiver
}

func (receiver *Params) Limit(limit int) *Params {
	receiver.Page(1, limit)
	return receiver
}

func (receiver *Params) Page(page, limit int) *Params {
	if receiver.Paging == nil {
		receiver.Paging = &Paging{Page: page, Limit: limit}
	} else {
		receiver.Paging.Page = page
		receiver.Paging.Limit = limit
	}
	return receiver
}
